/*
 * To change this template, choose Tools | Templates
 * AND open the template in the editor.
 */

package modelos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author usuario
 */
public class ModeloIngrediente extends ModeloDB{

    private int codigo;
    private float stock;

    public ModeloIngrediente() {
        super();
    }

    public boolean registar(){
        abrirConexion();
        boolean sw = false;
        String tira="INSERT INTO Ingrediente (codigo, descripcion, stock , estatus) VALUES("+getCodigo()+",'"+getDescripcion()+"',"+getStock()+",'A')";
       try {
            Statement stam = conexion.createStatement();
            stam.execute(tira);
            sw= true;
        } catch (SQLException ex) {
                   Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     
             sw= false;
        }
        
        cerrarConexion();
        return sw;
    }

  public boolean actualizar(){
     abrirConexion();
     boolean sw = false;
     String tira="UPDATE Ingrediente SET stock ="+getStock()+" WHERE codigo="+getCodigo()+"  AND estatus = 'A'";
     try {
          Statement stam = conexion.createStatement();
            stam.execute(tira);
            sw= true;
     } catch (SQLException ex) {
            Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
     cerrarConexion();
     return sw;
 }

  public boolean  consultar(){
    abrirConexion();
    boolean sw = false;
    String tira="SELECT * FROM Ingrediente WHERE codigo="+getCodigo()+" AND estatus='A'";
    try {
         Statement stam = conexion.createStatement();
            ResultSet rs =  stam.executeQuery(tira);
        if (rs.next()){
            sw = true;
            setDescripcion(rs.getString("descripcion"));
            setCodigo(rs.getInt("codigo"));
            setStatus(rs.getString("estatus"));
            setStock(rs.getFloat("stock"));
         }
    } catch (SQLException ex) {
        Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    return sw;
  }

  public LinkedList<ModeloIngrediente> consultarGeneral(){
    LinkedList<ModeloIngrediente> v = new LinkedList<ModeloIngrediente>();
    String tira="SELECT * FROM Ingrediente WHERE estatus='A'";
    abrirConexion();
    try {
        Statement stam = conexion.createStatement();
        ResultSet rs =  stam.executeQuery(tira);
        while (rs.next()){
            ModeloIngrediente  i = new ModeloIngrediente();
            i.setDescripcion(rs.getString("descripcion"));
            i.setCodigo(rs.getInt("codigo"));
            i.setStatus(rs.getString("estatus"));
            i.setStock(rs.getFloat("stock"));
            v.add(i);
         }
    } catch (SQLException ex) {
        Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    cerrarConexion();
    return v;
  }


  
  public boolean revisarDisponibilidadStock(float porcion){
      consultar();
      if((getStock()-porcion)>=0)
          return true;
      else
          return false;
  }

  public void restarStock(){
       abrirConexion();
       String tira="UPDATE Ingrediente SET stock ="+getStock()+" WHERE codigo="+getCodigo()+"  AND estatus = 'A'";
       try {
           Statement stam = conexion.createStatement();
           stam.execute(tira);
       } catch (SQLException ex) {
           Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
       }
       cerrarConexion();
  }

  public LinkedList<String []> ingredientesMasUsados(boolean asc, boolean desc){
    LinkedList<String []> v = new LinkedList<String []>();
    String tira="SELECT raciones , descripcion , codigo FROM  Ingrediente,(SELECT SUM(racion) AS raciones,cod_ing AS cod FROM IngredienteXProducto  GROUP BY cod_ing) Suma WHERE Ingrediente.codigo=Suma.cod ORDER BY raciones";
    if(asc)
        tira+= "ASC";
    if(desc)
        tira+= "DESC";
    abrirConexion();
    try {
        Statement stam = conexion.createStatement();
        ResultSet rs =        stam.executeQuery(tira);

        while (rs.next()){
            String [] s = {rs.getString("descripcion"), ""+rs.getFloat("cantidades"), ""+rs.getInt("id")};
            v.add(s);
        }
  } catch (SQLException ex) {
        Logger.getLogger(ModeloProducto.class.getName()).log(Level.SEVERE, null, ex);
    }
    return v;
  }


    public float getStock() {
        return stock;
    }

    public void setStock(float stock) {
        this.stock = stock;
    }

    public String getStatus() {
        return estatus;
    }

    public void setStatus(String estatus) {
        this.estatus = estatus;
    }


}
